package kuwo

import (
	"api-go/cache"
	"api-go/serializer"
	"api-go/util"
	"encoding/json"
	"fmt"
	"time"

	"github.com/go-redis/redis/v8"
)

// 歌单所需信息
type PlayList struct {
	Category string `form:"category"`
	Limit    string `form:"limit"`
	Offset   string `form:"offset"`
	Id       string `form:"id"`
	Order    string `form:"order"`
}

//获取歌单列表
func (service *PlayList) PlaylistList() *serializer.Response {

	if val := service.Category; val != "" {
		service.Category = val
	} else {
		service.Category = "146"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "20"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	var url string

	//若没有Order,则走第二个链接
	if val := service.Order; val != "" {
		service.Order = val
		url = "http://kuwo.cn/api/www/classify/playlist/getRcmPlayList?pn=?pn=" + service.Offset + "&rn=" + service.Limit + "&order=" + service.Order + "&httpsStatus=1&reqId=375c0740-4c1e-11eb-af49-1156a1732ca7"
	} else {
		//hot或者new
		service.Order = "hot"
		url = "https://kuwo.cn/api/www/classify/playlist/getTagPlayList?pn=" + service.Offset + "&rn=" + service.Limit + "&id=" + service.Category + "&httpsStatus=1&reqId=fe738170-4c1c-11eb-af49-1156a1732ca7"
	}
	fmt.Println(url)
	res, err := util.KuwoGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单信息
func (service *PlayList) PlaylistInfo() *serializer.Response {

	if val := service.Id; val != "" {
		service.Id = val
	} else {
		service.Id = "3127794871"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "20"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	url := "http://kuwo.cn/api/www/playlist/playListInfo?pn=" + service.Offset + "&rn=" + service.Limit + "&pid=" + service.Id + "&httpsStatus=1"

	res, err := util.KuwoGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取歌单分类
func (service *PlayList) PlaylistCat() *serializer.Response {
	//定义redis key和判断redis是否有该值
	redisKey := "kuwo-playlist-cat"
	redisResult, err := cache.RedisClient.Get(ctx, redisKey).Result()
	//若redis没有该值则执行获取
	if err == redis.Nil {
		url := "http://kuwo.cn/api/www/playlist/getTagList?httpsStatus=1"

		res, err := util.KuwoGet(url)
		if err != nil {
			return err
		}
		//将结果转为Json数据
		b, e := json.Marshal(res)
		if e != nil {
			fmt.Println(err)
		}
		//转为string存入redis
		e = cache.RedisClient.Set(ctx, redisKey, string(b), 24*time.Hour).Err()
		if e != nil {
			fmt.Println("err1", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}

		return serializer.OkResponse(res)
	} else if err != nil {
		fmt.Println("err2", err)
		return serializer.ErrorResponse(serializer.RedisError)
	} else {
		//将从redis获取的结果（string类型）转为map[string]inerface{}
		bodyMapData := map[string]interface{}{}
		err = json.Unmarshal([]byte(redisResult), &bodyMapData)
		if err != nil {
			fmt.Println("err3", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}
		return serializer.OkResponse(bodyMapData)
	}

}

//获取歌单推荐
func (service *PlayList) PlaylistRecommend() *serializer.Response {

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "20"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	//定义redis key和判断redis是否有该值
	redisKey := "kuwo-playlist-recommend-" + service.Limit + "-" + service.Offset
	redisResult, err := cache.RedisClient.Get(ctx, redisKey).Result()
	//若redis没有该值则执行获取
	if err == redis.Nil {
		url := "http://www.kuwo.cn/api/www/rcm/index/playlist?id=rec&pn" + service.Offset + "&rn=" + service.Limit + "&httpsStatus=1"

		res, err := util.KuwoGet(url)
		if err != nil {
			return err
		}
		//将结果转为Json数据
		b, e := json.Marshal(res)
		if e != nil {
			fmt.Println(err)
		}
		//转为string存入redis
		e = cache.RedisClient.Set(ctx, redisKey, string(b), 24*time.Hour).Err()
		if e != nil {
			fmt.Println("err1", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}

		return serializer.OkResponse(res)
	} else if err != nil {
		fmt.Println("err2", err)
		return serializer.ErrorResponse(serializer.RedisError)
	} else {
		//将从redis获取的结果（string类型）转为map[string]inerface{}
		bodyMapData := map[string]interface{}{}
		err = json.Unmarshal([]byte(redisResult), &bodyMapData)
		if err != nil {
			fmt.Println("err3", err)
			return serializer.ErrorResponse(serializer.RedisError)
		}
		return serializer.OkResponse(bodyMapData)
	}
}
